package md.lanjing.com.module_home.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lxh.baselibray.base.XActivity;
import com.lxh.baselibray.util.SPUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import md.lanjing.com.module_home.R;
import md.lanjing.com.module_home.R2;
import md.lanjing.com.module_home.ui.adapter.GuidePagerAdapter;

import com.lxh.baselibray.constant.Constant;


/**
 * 引导页
 */
public class GuideActivity extends XActivity {
    @BindView(R2.id.vp_guide)
    ViewPager vpGuide;
    @BindView(R2.id.dot_container)
    LinearLayout dotContainer;
    @BindView(R2.id.btn_next)
    Button btnNext;

    ImageView[] ivPointArr;

    private int[] imgIdArray = {R.drawable.welcome1, R.drawable.welcome2, R.drawable.welcome3};

    @Override
    public int getLayoutId() {
        return R.layout.activity_guide;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        SPUtils.putBoolean(Constant.FIRST_OPEN, false, this);
        initViewPage();
        initPoint();
    }

    private void initPoint() {
        ivPointArr = new ImageView[imgIdArray.length];
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30, 30);
        params.setMargins(20, 0, 20, 0);
        for (int i = 0; i < ivPointArr.length; i++) {
            ImageView ivDot = new ImageView(this);
            ivDot.setLayoutParams(params);
            ivPointArr[i] = ivDot;
            if (i == 0) {
                ivDot.setBackgroundResource(R.drawable.shape_guide_select_dot);
            } else {
                ivDot.setBackgroundResource(R.drawable.shape_guide_unselect_dot);
            }
            dotContainer.addView(ivPointArr[i]);
        }
    }

    private void initViewPage() {
        List<View> viewList = new ArrayList<>();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        for (int i = 0; i < imgIdArray.length; i++) {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(params);
            iv.setBackgroundResource(imgIdArray[i]);
            viewList.add(iv);
        }

        GuidePagerAdapter adapter = new GuidePagerAdapter(viewList);
        vpGuide.setAdapter(adapter);

        vpGuide.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {

                for (int j = 0; j < imgIdArray.length; j++) {
                    ivPointArr[j].setBackgroundResource(R.drawable.shape_guide_select_dot);
                    if (i != j) {
                        ivPointArr[j].setBackgroundResource(R.drawable.shape_guide_unselect_dot);
                    }
                }

                if (i == imgIdArray.length - 1) {
                    btnNext.setVisibility(View.VISIBLE);
                } else {
                    btnNext.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }


    @OnClick(R2.id.btn_next)
    public void onViewClicked() {
        startActivity(new Intent(this, LoginActivity.class));
        finish();
    }

}
